
package states;

import entities.PlayableChar;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import management.BotoMenu;
import management.DBAccess;
import management.ObjectDispatcher;
import management.Partida;
import management.ZoneBuilder;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class LoadGameState extends BasicGameState {

    public static final int ID = 4;
    private GameContainer container;
    private StateBasedGame state;
    private BotoMenu slot;
    private HashMap<Integer, Partida> partides = new HashMap<Integer, Partida>();
    private Image imatgeFons;
    private Image imatgeSlot;
    private Image imatgeSlotOver;
    private Image rightArrow;
    private Image leftArrow;
    private Image rightArrowOver;
    private Image leftArrowOver;
    private Partida partida;
    private BotoMenu fletxaDreta;
    private BotoMenu fletxaEsquerra;
    private int indexPartida;
    ResultSet rs;
    Image RogueImg;
    DBAccess dba = new DBAccess();

    @Override
    public int getID() {
        return ID;
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        this.container = gc;
        this.state = sbg;
        imatgeFons = dba.recuperarMenuResource("fonsLoad");
        imatgeSlot = dba.recuperarMenuResource("slot");
        imatgeSlotOver = dba.recuperarMenuResource("slotOver");
        RogueImg = dba.recuperarMenuResource("fotoRogue");
        rightArrow = dba.recuperarMenuResource("rightArrow");
        leftArrow = dba.recuperarMenuResource("leftArrow");
        rightArrowOver = dba.recuperarMenuResource("rightArrowOver");
        leftArrowOver = dba.recuperarMenuResource("leftArrowOver");
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        imatgeFons.draw(0, 0);
        slot.render(gc, grphcs);
        fletxaDreta.render(container, grphcs);
        fletxaEsquerra.render(container, grphcs);
        grphcs.drawImage(RogueImg, 330, 270);
        grphcs.drawImage(RogueImg, 470, 270);
        grphcs.drawImage(RogueImg, 330, 300);
        grphcs.drawImage(RogueImg, 470, 300);
        grphcs.setColor(org.newdawn.slick.Color.yellow);
        grphcs.scale(1.1f, 1.1f);
        grphcs.drawString("Id partida: " + partides.get(indexPartida).getIdPartida(), 160, 250);
        grphcs.drawString("Exp." + partides.get(indexPartida).getExpJ1(), 335, 250);
        grphcs.drawString("Exp." + partides.get(indexPartida).getExpJ2(), 460, 250);
        grphcs.drawString("Zona: " + partides.get(indexPartida).getZone(), 160, 275);
        grphcs.drawString("Exp." + partides.get(indexPartida).getExpJ3(), 335, 275);
        grphcs.drawString("Exp." + partides.get(indexPartida).getExpJ4(), 460, 275);
        grphcs.drawString("Exp." + partides.get(indexPartida).getData(), 160, 300);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {

        if (gc.getInput().isKeyPressed(Input.KEY_ESCAPE)) {
            state.enterState(MainMenuState.ID);
        }
        slot.setActiu(true);
        fletxaDreta.setActiu(true);
        fletxaEsquerra.setActiu(true);
    }

    @Override
    public void enter(GameContainer gc, StateBasedGame state) {
        indexPartida = 1;
        omplirPartides();
        crearFletxes();
        crearSlot();
    }

    private void omplirPartides() {
        try {
            int idPartida;
            int zone;
            int expJ1;
            int expJ2;
            int expJ3;
            int expJ4;
            String data;
            rs = dba.carregarPartides();

            while (rs.next()) {
                idPartida = rs.getInt(1);
                zone = rs.getInt(2);
                expJ1 = rs.getInt(3);
                expJ2 = rs.getInt(4);
                expJ3 = rs.getInt(5);
                expJ4 = rs.getInt(6);
                data = rs.getString(7);
                partida = new Partida(idPartida, zone, expJ1, expJ2, expJ3, expJ4, data);
                partides.put(rs.getRow(), partida);
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoadGameState.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void crearSlot() {
        slot = new BotoMenu(container, imatgeSlot, 100, 230);
        slot.setMouseOverImage(imatgeSlotOver);
        slot.addListener(new ComponentListener() {

            public void componentActivated(AbstractComponent source) {
                ZoneBuilder zb = new ZoneBuilder();
                ObjectDispatcher od = new ObjectDispatcher();
                try {

                    int z = partides.get(indexPartida).getZone();
                    zb.buildZone(z);
                    ObjectDispatcher.setZone(z);
                    od.crearHashMapPnjs();
                    ObjectDispatcher.getPnjs().get("Soldier").setExp(partides.get(indexPartida).getExpJ1());
                    ObjectDispatcher.getPnjs().get("BountyHunter").setExp(partides.get(indexPartida).getExpJ2());
                    ObjectDispatcher.getPnjs().get("Rogue").setExp(partides.get(indexPartida).getExpJ3());
                    ObjectDispatcher.getPnjs().get("Wolf").setExp(partides.get(indexPartida).getExpJ4());
                    ObjectDispatcher.setIdPartida(partides.get(indexPartida).getIdPartida());
    
                     state.enterState(GamePlayState.ID);
                } catch (SQLException ex) {
                    Logger.getLogger(LoadGameState.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    private void crearFletxes() {
        fletxaDreta = new BotoMenu(container, rightArrow, 580, 430);
        fletxaDreta.setMouseOverImage(rightArrowOver);
        fletxaDreta.addListener(new ComponentListener() {

            public void componentActivated(AbstractComponent source) {
                if (indexPartida < partides.size()) {
                    indexPartida += 1;
                }
            }
        });
        fletxaEsquerra = new BotoMenu(container, leftArrow, 120, 430);
        fletxaEsquerra.setMouseOverImage(leftArrowOver);
        fletxaEsquerra.addListener(new ComponentListener() {

            public void componentActivated(AbstractComponent source) {
                if (indexPartida > 1) {
                    indexPartida -= 1;

                }
            }
        });
    }


}